package io.github.rascaler.dcenter.platforms.binance;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.github.rascaler.dcenter.platforms.Parser;
import io.github.rascaler.dcenter.pojo.Instrument;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName BinanceInstrumentParser
 * @Description TODO
 * @Author wurenqing
 * @Date 2023-12-08 10:45
 */
@Component("binanceInstrumentParser")
public class BinanceInstrumentParser implements Parser {
    @Override
    public List<Instrument> parse(Object origin) {
        if (null == origin) {
            return null;
        }
        List<Instrument> result = new ArrayList<>();
        JSONObject obj = JSON.parseObject(origin.toString());
        obj.getJSONArray("symbols").forEach(s -> {
            JSONObject symbol = (JSONObject) s;
            if (!symbol.getString("status").equals("TRADING") ||
                    !symbol.getString("contractType").equals("PERPETUAL") ||
            !symbol.get("quoteAsset").equals("USDT")) {
                return;
            }
            Instrument instrument = new Instrument();
            instrument.setSymbol(symbol.getString("symbol"));
            instrument.setName(symbol.getString("symbol"));
            instrument.setBase(symbol.getString("baseAsset"));
            instrument.setQuote(symbol.getString("quoteAsset"));
            instrument.setOnboardDate(symbol.getLong("onboardDate"));
            instrument.setStatus(symbol.getString("status"));
            instrument.setExchangeId(1);
            //   {"amount": 3, "price": 1, "base": 8, "quote": 8}
            Instrument.Precision precision = new Instrument.Precision(symbol.getLong("quantityPrecision"),
                    symbol.getLong("pricePrecision"),
                    symbol.getLong("baseAssetPrecision"),
                    symbol.getLong("quotePrecision"));
            instrument.setPrecision(precision);

            Instrument.Limit limit = new Instrument.Limit();
            symbol.getJSONArray("filters").stream().filter(l -> ((JSONObject)l).get("filterType").equals("LOT_SIZE")).findFirst().ifPresent(l -> {
                JSONObject lotSize = (JSONObject) l;
                Instrument.Range amount = new Instrument.Range();
                amount.setMin(lotSize.getBigDecimal("minQty"));
                amount.setMax(lotSize.getBigDecimal("maxQty"));
                limit.setAmount(amount);
            });
            symbol.getJSONArray("filters").stream().filter(l -> ((JSONObject)l).get("filterType").equals("PRICE_FILTER")).findFirst().ifPresent(l -> {
                JSONObject priceFilter = (JSONObject) l;
                Instrument.Range price = new Instrument.Range();
                price.setMin(priceFilter.getBigDecimal("minPrice"));
                price.setMax(priceFilter.getBigDecimal("maxPrice"));
                limit.setPrice(price);
            });
            instrument.setLimits(limit);
            result.add(instrument);
        });
        return result;
    }
}
